<template>
  <div class="app-container">
    <el-button type="primary" @click="$router.push({name: 'Process'})">返回</el-button>
    <el-table v-loading="loading" :data="nodes" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="节点类型" align="center" prop="type" />
      <el-table-column label="节点名称" align="center" prop="name" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.type===1"
            icon="el-icon-magic-stick"
            @click="toSet(scope.row)"
          >设置审核人</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改模型配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="560px">
      <el-form :form="form">
        <el-form-item>
          <el-alert :closable="false" show-icon title="温馨提示：若流程运行至未分配审批人员的审批节点时，流程将自动中断取消" type="warning"></el-alert>
        </el-form-item>
        <el-form-item label="节点名称">
          <span>{{this.nodeName}}</span>
        </el-form-item>
        <el-form-item label="审批人：">
          <el-checkbox @change="roleChecked=true, type = 1">角色</el-checkbox>
          <el-checkbox @change="deptChecked=true, type = 2">部门</el-checkbox>
          <el-checkbox @change="userChecked=true, type = 3">用户</el-checkbox>
          <el-checkbox @change="type = 4">所属部门负责人</el-checkbox>
        </el-form-item>
        <el-form-item label="选择角色：" v-show="roleChecked" prop="role">
          <el-select v-model="form.roles" multiple placeholder="角色">
            <el-option
              v-for="item in roleOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              :disabled="item.status == 1"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择部门：" v-show="deptChecked">
          <treeselect v-model="form.deptId" style="width: 20vw" :options="deptOptions" :normalizer="normalizer" placeholder="请选择归属机构" />
        </el-form-item>
        <el-form-item label="选择用户：" v-show="userChecked">
          <el-select v-model="form.user" multiple placeholder="用户">
            <el-option
              v-for="item in userOptions"
              :key="item.id"
              :label="item.nickName"
              :value="item.id"
              :disabled="item.status == 1"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listModel, getModel, delModel, addModel, deployModel } from "@/api/activiti/model";
  import { listRole } from '@/api/system/role'  //一定要用大括号接收函数，否则报错
  import { listUser } from '@/api/system/user'
  import { deptTree } from "@/api/system/dept";
  import { getNodeInfo, getNodeList, updateNodeInfo} from "@/api/activiti/node";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "Node2",
    components: { Treeselect },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        nodes: [],
        type: null,
        roleChecked: false,
        roleOptions: [],
        deptChecked: false,
        deptOptions: [],
        userChecked: false,
        userOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 模型分类数据字典
        categoryOptions: [],
        // 查询模型
        queryParams: {
          current: 1,
          size: 10,
          name: undefined,
          category: undefined,
          key: undefined
        },
        // 显示搜索条件
        showSearch: true,
        // 表单模型
        form: {},
        nodeId: null,
        nodeName: null
      };
    },
    watch: {
      type() {
        console.log("type==="+this.type)
      }
    },
    created() {
      this.getList();
      this.getDicts("model_category").then(response => {
        this.categoryOptions = response.data;
      });
      //注意这里接收路由传递参数的“$route”没有“r”的
      const procDefId = this.$route.query.procDefId
      console.log("node parocDefId==="+procDefId)
      getNodeList(procDefId).then(res => {
        this.nodes = res.data.rows.map(n => {
          let icon = ''
          switch (n.type) {
            case 0:
              icon = 'icon-kaishi1'
              break
            case 2:
              icon = 'icon-jieshu'
              break
            default:
              icon = 'icon-shen'
          }
          return { name: n.name, nodeId: n.nodeId, icon: icon, type: n.type }
        })
      })
      listRole().then(
        resp => {
          this.roleOptions = resp.data;
        }
      )
      deptTree().then(response => {
        this.deptOptions = response.data;
      });
      listUser().then(res => {
        this.userOptions = res.data;
      })
    },
    methods: {
      /** 转换机构数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.name,
          children: node.children
        };
      },
      toSet(row) {
        this.open = true;
        this.nodeId = row.nodeId;
        this.nodeName = row.name;
      },
      /** 查询模型列表 */
      getList() {
        this.loading = true;
        listModel(this.queryParams).then(response => {
            this.modelList = response.data;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      // 模型分类字典翻译
      categoryFormat(row, column) {
        return this.selectDictLabel(this.categoryOptions, row.category);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        //this.reset();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 提交按钮 */
      submitForm: function() {
        const reqBody = {
          nodeId: this.nodeId,
          roleIds: this.form.roles,
          deptIds: this.form.deptId,
          userIds: this.form.user,
          type: this.type
        }
        console.log("form==", this.form)
        updateNodeInfo(reqBody).then(response => {
          if (response.code === 0) {
            this.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          } else {
            this.msgError(response.msg);
          }
        });
      },
      /** 部署按钮操作 */
      handleDeploy(row){

      }
    }
  };
</script>
